<?php
namespace app\index\controller;
use app\model\Contact as ContactModel;
use think\Db;


class Contact extends Base
{
    public function __construct(){
        $model = new ContactModel();
        $this->model = $model;
        parent::__construct();
        
        $page_info = $this->page_info;
        $page_info['title'] = '联系';

        $this->assign([
            'model'=>$this->model,
            'page_info'=>$page_info
        ]);
    }

    public function save(){
        if (request()->isPost()) {
            $fields = [
                'must'=>['truename','mobile'],
                'nomust'=>['email','address','company']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                // $this->error($save_data['error_msg']);
                return json(['type'=>'danger','message'=>$save_data['error_msg']]);
            }
            if (!isMobile($save_data['mobile'])) {
                return json(['type'=>'danger','message'=>'请输入正确的手机号']);
            }
            $save_data['ctime'] = time();
            $save_data['ip_address'] = request()->ip();
            $model = $this->model;
            if ( $model->save($save_data) ) {
                // $this->success("提交成功，客服将尽快与你联系");
                return json(['type'=>'success','message'=>'提交成功，客服将尽快与你联系']);
            }else{
                // $this->error("添加失败");
                return json(['type'=>'danger','message'=>'添加失败']);
            }
        }
        return view('save', [
            
        ]);
    }

     public function saveFooter(){
        if (request()->isPost()) {
            $fields = [
                'must'=>['contact_txt'],
                'nomust'=>[]
            ];
            $params = paramFilter(request()->param(),$fields);
            if (!empty($params['error_msg'])) {
                $this->error($params['error_msg']);
            }
            if (!isMobile($params['contact_txt']) && !isEmail($params['contact_txt'])) {
                $this->error('请输入正确的邮箱或者手机号');
            }

            $save_data = [];
            if (isMobile($params['contact_txt'])) {
                $save_data['mobile'] = $params['contact_txt'];
            }else{
                $save_data['email'] = $params['contact_txt'];
            }
            $save_data['ctime'] = time();
            $save_data['ip_address'] = request()->ip();
            $model = $this->model;
            if ( $model->save($save_data) ) {
                $this->success("提交成功，客服将尽快与你联系");
            }else{
                $this->error("添加失败");
            }
        }
    }

    
}
